//
//  MYXMyBoossViewController.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/4.
//  Copyright © 2017年 hbjApple. All rights reserved.
//我是boss

#import "MYXMyBoossViewController.h"
#import <MJRefresh.h>

#import "MYY_walletNavView.h"
#import "MYXMyBoossCell.h"
#import "MYXMyBoossTopView.h"
#import "MYXMyBoossHeadView.h"
#import "M80AttributedLabel.h"
#import "MYXAgreementViewController.h"
#import "MYXProfitDealViewController.h"
#import "MYXbossModel.h"
#import "MYX_Bossson_listModel.h"
#import "MYCIndexCardViewController.h"

static NSString *const MYXMyBoossCellIdentifier = @"MYXMyBoossCell";
static NSString *const MYXMyBoossTopViewIdentifier = @"MYXMyBoossTopView";
static NSString *const MYXMyBoossHeadViewIdentifier = @"MYXMyBoossHeadView";

@interface MYXMyBoossViewController ()<UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIAlertViewDelegate,M80AttributedLabelDelegate>{
     BOOL  isNo;
    NSInteger page;
    NSInteger size;
}
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)MYY_walletNavView *MYY_walletNavVi;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)MYXMyBoossTopView *MYXMyBoossTopV;
@property (nonatomic, strong)MYXMyBoossHeadView *MYXMyBoossHeadVi;

@property(nonatomic, strong) M80AttributedLabel *AgreementLabel;

@property(nonatomic, strong) MYXHttpManager *MYXHttpManag;
@property(nonatomic, strong)MYXbossModel  *MYXbossMod;



@end

@implementation MYXMyBoossViewController

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

//导航栏
-(void)initNavgationBar
{
    
    WS(weakSelf)
    self.MYY_walletNavVi =[[MYY_walletNavView alloc]init];
    self.MYY_walletNavVi.frame = CGRectMake(0, 0, _tableView.frame.size.width,SafeAreaTopHeight);
    [self.view addSubview:self.MYY_walletNavVi];
    self.MYY_walletNavVi.backButton =^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    [self.MYY_walletNavVi.rightBtn setTitle:@"收益明细" forState:UIControlStateNormal];
    self.MYY_walletNavVi.rightButton =^{
        MYXProfitDealViewController *MYXProfitDealV =[[MYXProfitDealViewController alloc]init];
        MYXProfitDealV.navigationItem.title = @"收益明细";
        [weakSelf.navigationController pushViewController:MYXProfitDealV animated:YES];
    };
    self.MYY_walletNavVi.nameLable.hidden = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AdjustsScrollViewInsetNever(self, self.tableView);
    self.MYXHttpManag =[[MYXHttpManager alloc]init];
   // self.MYXbossMod = [[MYXbossModel alloc]init];
    [self.tableView.mj_header beginRefreshing];
    page = 1;
    size = 5;
    [self initBossRequest];
    isNo = NO;
    [self createUI];
    [self addrefresh];
}
#pragma mark - UI
- (void)addrefresh{
    WS(weakSelf);
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [weakSelf initBossRequest];
        
    }];
}
- (void)createUI
{
    
    WS(weakSelf)
    
    
    self.view.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    self.tableView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:MYXMyBoossCellIdentifier bundle:nil] forCellReuseIdentifier:MYXMyBoossCellIdentifier];
     self.MYXMyBoossTopV                                =[[NSBundle mainBundle]loadNibNamed:MYXMyBoossTopViewIdentifier owner:self options:nil][0];
    self.MYXMyBoossTopV.frame = CGRectMake(0, 0, _tableView.frame.size.width,300);
    self.tableView.tableHeaderView =self.MYXMyBoossTopV;
    [self initNavgationBar];
    [self bottom];
    self.MYXMyBoossTopV.TotalIncomeB =^(UIButton *btn){
        
        [weakSelf usefrozenmoney];
    };
    
    
    
}


-(void)bottom{
    [self.view addSubview:self.AgreementLabel];
    [self.AgreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.width.equalTo(self.view);
        make.left.equalTo(self.view).with.offset(0);
        make.height.equalTo(@20);
        make.right.equalTo(self.view).with.offset(0);
    }];
    
}
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

#pragma mark - tableview datasource and delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (isNo) {
        return self.MYXbossMod.son_list.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MYXMyBoossCell *MYXMyBoossCe = (MYXMyBoossCell *)[tableView dequeueReusableCellWithIdentifier:MYXMyBoossCellIdentifier];
    MYXMyBoossCe.selectionStyle = UITableViewCellSelectionStyleNone;
    MYXMyBoossCe.rightLabel.hidden = YES;
    MYXMyBoossCe.lineView.hidden = YES;
    MYX_Bossson_listModel *MYX_Bossson_listMod =self.MYXbossMod.son_list[indexPath.row];
    MYXMyBoossCe.MYX_Bossson_listM =MYX_Bossson_listMod;
    
    return MYXMyBoossCe;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MYX_Bossson_listModel *model = self.MYXbossMod.son_list[indexPath.row];
    MYCIndexCardViewController * vc = [[MYCIndexCardViewController alloc]init];
    vc.userID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return MYY_wallet_cell_H;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
     WS(weakSelf)
    self.MYXMyBoossHeadVi                                =[[NSBundle mainBundle]loadNibNamed:MYXMyBoossHeadViewIdentifier owner:self options:nil][0];
    self.MYXMyBoossHeadVi.frame = CGRectMake(0, 0, WIDTH,30);
    self.MYXMyBoossHeadVi.bu.selected =isNo;
    if (self.MYXMyBoossHeadVi.bu.selected) {
        [self.MYXMyBoossHeadVi.bu  setImage:[UIImage imageNamed:@"arrow_right"] forState:UIControlStateNormal];
    }else{
         [self.MYXMyBoossHeadVi.bu  setImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    }
    self.MYXMyBoossHeadVi.Butt = ^(UIButton *btn){
        btn.selected = !btn.selected;
        if (btn.selected) {
            isNo = YES;
        }else{
            isNo = NO;
        }
        [weakSelf.tableView reloadData];
        
    };
    self.MYXMyBoossHeadVi.numLabel.text = [NSString stringWithFormat:@"%ld/100",self.MYXbossMod.son_list.count];
    return self.MYXMyBoossHeadVi;
}
-(CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *fooerView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 0.1)];
    fooerView.backgroundColor =MYYlineColor;
    
    return fooerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}


- (M80AttributedLabel *)AgreementLabel
{
    if (_AgreementLabel == nil) {
        _AgreementLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _AgreementLabel.font = [UIFont systemFontOfSize:12];
        _AgreementLabel.textColor = MYYheadColor;
        _AgreementLabel.textAlignment = kCTTextAlignmentCenter;
        _AgreementLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        _AgreementLabel.numberOfLines = 1;
        _AgreementLabel.delegate = self;
        [_AgreementLabel setText:@"邀请雇主加入获取丰厚利益"];
        [_AgreementLabel addCustomLink:@"Agreement" forRange:NSMakeRange(0, 12) linkColor:MYYheadColor];
    }
    return _AgreementLabel;
}

#pragma mark - M80Attribute delegate
- (void)m80AttributedLabel:(M80AttributedLabel *)label
             clickedOnLink:(id)linkData
{
    if ([linkData isEqualToString:@"Agreement"]) {
        MYXAgreementViewController *MYXAgreementViewContro =[[MYXAgreementViewController alloc]init];
        MYXAgreementViewContro.navigationItem.title = @"雇主加入";
        [self.navigationController pushViewController:MYXAgreementViewContro animated:YES];
    }
  
}


#pragma mark - 我是boss接口

-(void)initBossRequest{
    NSString *pagestring = [NSString stringWithFormat:@"%ld",page];
    NSString *sizestring = [NSString stringWithFormat:@"%ld",size];
    [self.MYXHttpManag boss:^(MYXbossModel *MYXbossMod) {
        if (page == 1) {
            self.MYXbossMod = [[MYXbossModel alloc] initWithModel:MYXbossMod];
        }else{
            if (MYXbossMod.son_list.count == size) {
                [self.MYXbossMod.son_list addObjectsFromArray:MYXbossMod.son_list];
                [self.tableView.mj_footer endRefreshing];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
          
        }
        if (MYXbossMod) {
            if (self.MYXbossMod.frozen_money.length<=0||[self.MYXbossMod.frozen_money floatValue]<=0) {
                 self.MYXMyBoossTopV.titileLabel.text = [NSString stringWithFormat:@"0元"];
                 self.MYXMyBoossTopV.UnclaimedLabel.text = [NSString stringWithFormat:@"0元"];
                 self.MYXMyBoossTopV.TotalIncomeButton.enabled =NO;
                [self.MYXMyBoossTopV.TotalIncomeButton setBackgroundColor:MYYColorFromRGB(0xA6A6A6)];
            }else{
                 self.MYXMyBoossTopV.titileLabel.text = [NSString stringWithFormat:@"%@元",self.MYXbossMod.frozen_money];
                self.MYXMyBoossTopV.UnclaimedLabel.text = [NSString stringWithFormat:@"%@元",self.MYXbossMod.frozen_money];
                self.MYXMyBoossTopV.TotalIncomeButton.enabled =  YES;
            }
            
            
            if (self.MYXbossMod.shouyi_zong.length<=0) {
                self.MYXMyBoossTopV.TotalIncomeLabel.text = [NSString stringWithFormat:@"0元"];
            }else{
                self.MYXMyBoossTopV.TotalIncomeLabel.text = [NSString stringWithFormat:@"%@元",self.MYXbossMod.shouyi_zong];
            }
            [self.tableView reloadData];
        }
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
         [JXTHUDHelper showHUDText:@"网络出错了～" type:@"error" inView:nil];
    }
                       page:pagestring
                       size:sizestring];
}

-(void)usefrozenmoney{
    [self.MYXHttpManag success:^(NSString *dataStr) {
        [JXTHUDHelper showHUDText:@"领取成功" type:@"success" inView:nil];
        [self initBossRequest];
    } failure:^(NSString *errorCode) {
        [JXTHUDHelper showHUDText:errorCode type:@"error" inView:nil];
    } netDown:^{
        [JXTHUDHelper showHUDText:@"网络出错了～" type:@"error" inView:nil];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
